package com.example.robotica.appmobile.activities;

import org.apache.commons.net.telnet.TelnetClient;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import ar.com.untref.robotica.appmobile.R;

import com.example.robotica.appmobile.utils.Controlador;

public class PrincipalActivity extends FragmentActivity{
	
	private TextView consola;
	private EditText editDeDireccionServer;
	private Editable servidor;
	private EditText comando, puerto;
	private Button botonConexion, botonDeComandos, botonControlManual;
	private TelnetClient telnet = new TelnetClient();
	private Controlador controladorTelnet;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        controladorTelnet = Controlador.getInstancia();
        controladorTelnet.setClienteTelnet(telnet);

		consola = (TextView) findViewById(R.id.consola);
		editDeDireccionServer = (EditText) findViewById(R.id.servidor);
		comando = (EditText) findViewById(R.id.edit_comandos);
		puerto = (EditText) findViewById(R.id.puerto);
		botonConexion = (Button) findViewById(R.id.boton_conexion);
		botonDeComandos = (Button) findViewById(R.id.boton_de_comandos);
		botonControlManual = (Button) findViewById(R.id.boton_control_manual);
		
		botonConexion.setOnClickListener(conectarODesconectar);
		botonDeComandos.setOnClickListener(enviarComando);
		botonControlManual.setOnClickListener(activarControlManual);
		consola.setText("Android Socket" + "\n");
		
		SharedPreferences preferencias = getSharedPreferences("app", MODE_PRIVATE);
		editDeDireccionServer.setText(preferencias.getString("servidor", null));
		puerto.setText(preferencias.getString("puerto", null));
		
		servidor = editDeDireccionServer.getEditableText();
	}

	private OnClickListener conectarODesconectar = new OnClickListener() {
		
		@Override
		public void onClick(View v) {

			consola.setText("Android Socket" + "\n");
			if (botonConexion.getText().toString().equalsIgnoreCase(getString(R.string.conectar))){
				
				conectarSocket();
			} else {
				
				desconectarSocket();
			}
		}
	};

	private OnClickListener enviarComando = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			controladorTelnet.enviarComando(comando.getText().toString() + "\r\n");
			comando.setText("");
		}
	};
	
	private OnClickListener activarControlManual = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			Intent intent = new Intent(getApplicationContext(), ControlManualActivity.class);
			startActivity(intent);
		}
	};

	private void conectarSocket() {
		try {
			controladorTelnet.conectarSocket(servidor.toString(), Integer.parseInt(puerto.getText().toString()));
			consola.setText(getString(R.string.conexion_establecida));
			botonConexion.setText(getString(R.string.desconectar));
			botonConexion.setBackgroundColor(getResources().getColor(R.color.rojo_crema));
//			comando.setVisibility(View.VISIBLE);
//			botonDeComandos.setVisibility(View.VISIBLE);
			botonControlManual.setVisibility(View.VISIBLE);
			
			SharedPreferences.Editor editor = getSharedPreferences("app", MODE_PRIVATE).edit();
			editor.putString("servidor", servidor.toString());
			editor.putString("puerto", puerto.getText().toString());
			editor.commit();
			
			ocultarTeclado();
			
		} catch (Exception e) {
			e.printStackTrace();
			consola.setText(getString(R.string.no_se_pudo_conectar));
		}
	}

	private void ocultarTeclado() {

		InputMethodManager inputManager = (InputMethodManager)
		        getSystemService(Context.INPUT_METHOD_SERVICE); 

		inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
		           InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public void desconectarSocket() {
		try {
			controladorTelnet.desconectarSocket();
			botonConexion.setText(getString(R.string.conectar));
			botonConexion.setBackgroundColor(getResources().getColor(R.color.gris));
			consola.setText("");
			comando.setVisibility(View.GONE);
			botonDeComandos.setVisibility(View.GONE);
			botonControlManual.setVisibility(View.GONE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
